import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { getToken } from "@/utils/auth"
import { computed } from 'vue'
const routes = [
  {
    path: '/',
    name: 'home',
    components: {
      router_app: () => import('@/views/Home.vue'),
    },
  },
  {
    path: '/about',
    name: 'about',
    components: {
      router_app: () => import('@/views/About.vue'),
    },
  },
  {
    path: '/login',
    name: 'login',
    components: {
      router_app: () => import('@/views/Login.vue'),
    },
  },
  {
    path: '/employee',
    name: 'employee',
    components: {
      router_app: () => import('@/views/layout/index.vue'),
    },
    children: [
      {
        path: 'info',
        components: {
          router_main_container: () => import('@/views/layout/employee/employee.vue'),
        },
      },

    ]
  },
  {
    path: '/am_system',
    name: 'am_system',
    components: {
      router_app: () => import('@/views/layout/index.vue'),
    },
    children: [
      {
        path: 'list-attendence',
        components: {
          router_main_container: () => import('@/views/layout/attendance/ListAttendence.vue'),
        },
      },
      {
        path: 'uploadfile',
        components: {
          router_main_container: () => import('@/views/layout/file/Upload.vue'),
        },
      },
      {
        path: 'downloadfile',
        name: 'download',
        components: {
          router_main_container: () => import('@/views/layout/file/Download.vue'),
        }
      },
      {
        path: 'list-leave',
        name: 'list-leave',
        components: {
          router_main_container: () => import('@/views/layout/attendance/ListLeave.vue'),
        }
      },
    ]
  },
  {
    path: '/settings',
    name: 'settings',
    components: {
      router_app:()=>import("@/views/layout/index.vue"),
    },
    children: [
        {
        path: 'reset',
        components: {
          router_main_container: () => import('@/views/layout/settings/ResetPassword.vue'),
        }
      },
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  //获取本地token
  const token = getToken()

  console.log(token)
  //如果已登录
  if (token) {
    //如果要去登录
    if (to.name == 'login') {
      //禁止重复登录
      next('am_system')
      return
    } else {
      //其他页面正常跳转
      next()
      return
    }
  } else {
    if (to.name == 'login') {
      next()
      return
    }else{
      //没有登录则跳转到登录页面
      next('login')
      return
    }

  }
})


export default router
